package com.qire.manhua.model.viewModel.impl;

import com.qire.common.support.SmartRefreshViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.adapter.auto.SummerRecyclerBindAdapter;
import com.qire.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.controller.RecommendMoreController;
import com.qire.manhua.dal.service.RecommendMoreService;
import com.qire.manhua.databinding.LayoutRecommendMoreItmeBinding;
import com.qire.manhua.model.entity.ComicBookEntity;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.LayoutManager;

import java.util.ArrayList;

public class RecommendMoreViewModel extends SmartRefreshViewModel implements IViewModel {

    private RecommendMoreService moreService = new RecommendMoreService(this);

    private final ArrayList<RecommendMoreItemViewModel> moreItemList = new ArrayList();
    private final SummerRecyclerBindAdapter moduleAdapter = new SummerRecyclerBindAdapter(moreItemList);

    private int moduleId;

    private int pageNo = 1;

    @Override
    public int getVariableId() {
        return BR.recommendMoreViewModel;
    }

    //region    #附加管理 begin
    /**
     * 附加属性绑定
     */
    public void onAddition(RecommendMoreController controller) {
        moduleAdapter.setEventProcess(controller);
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，
     * 页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
        moduleAdapter.setEventProcess(null);
    }
    //endregion #附加管理 end

    //region    #业务数据 begin

    public void setModuleId(int moduleId) {
        this.moduleId = moduleId;
    }

    public int moduleId() {
        return moduleId;
    }

    public void nextPage(){
        this.pageNo++;
    }

    public int pageNo() {
        return this.pageNo;
    }

    private void resetPage() {
        this.pageNo = 1;
        clearMoreItemList();
    }

    public void loadMoreData() {
        resetPage();
        moreService.loadPageData();
    }

    public void refreshMoreData() {
        moreService.loadPageData();
    }

    private void clearMoreItemList() {
        moduleAdapter.notifyItemRangeRemoved(0, moreItemList.size());
        moreItemList.clear();
    }

    public void addMoreItem(int comicId, String coverImg, String floatTips, String title, String author,
                            String comicBrief, CharSequence label, String popularityValue) {
        moreItemList.add(new RecommendMoreItemViewModel(comicId, coverImg, floatTips, title, author, comicBrief, label, popularityValue));
    }

    public void refreshModuleAdapter() {
        moduleAdapter.notifyItemChanged(0, "full");
    }

    //endregion #业务数据 end

    // region #UI属性绑定
    public MutableLiveData<CharSequence> getModuleTitle() {
        return get("moduleTitle");
    }

    public Adapter getModuleAdapter() {
        return moduleAdapter;
    }

    public LayoutManager getModuleLayoutManager() {
        return new LinearLayoutManager(null);
    }
    // endregion

    @AdapterEntityBind(viewRid = R.layout.layout_recommend_more_itme)
    public class RecommendMoreItemViewModel extends BaseObservable implements AutoVariable<LayoutRecommendMoreItmeBinding> {

        private final int comicId;
        private final String coverImg;
        private final String floatTips;
        private final String title;
        private final String author;
        private final String comicBrief;
        private final CharSequence label;
        private final String popularityValue;

        public RecommendMoreItemViewModel(int comicId, String coverImg, String floatTips, String title, String author,
                                          String comicBrief, CharSequence label, String popularityValue) {
            this.comicId = comicId;
            this.coverImg = coverImg;
            this.floatTips = floatTips;
            this.title = title;
            this.author = author;
            this.comicBrief = comicBrief;
            this.label = label;
            this.popularityValue = popularityValue;
        }

        public ComicBookEntity toComicBookEntity() {
            return new ComicBookEntity(comicId, title);
        }

        @Override
        public void bindVariable(LayoutRecommendMoreItmeBinding viewBind) {
            viewBind.setRecommendMoreItemViewModel(this);
        }

        @Bindable
        public String getCoverPath() {
            return coverImg;
        }

        @Bindable
        public CharSequence getFloatTips() {
            return floatTips;
        }

        @Bindable
        public CharSequence getTitle() {
            return title;
        }

        @Bindable
        public CharSequence getAuthor() {
            return author;
        }

        @Bindable
        public CharSequence getComicBrief() {
            return comicBrief;
        }

        @Bindable
        public CharSequence getLabel() {
            return label;
        }

        @Bindable
        public CharSequence getPopularityValue() {
            return popularityValue;
        }

    }

}
